import { constantRoutes, asyncRouterMap } from '@/router'
import { listUserMenu } from '@/api/user'

const permission = () => {
  return {
    routers: constantRoutes,
    addRouters: [],
    menu_list: [],
    but_list: []
  }
}

const state = permission()

const mutations = {
  SET_ROUTERS: (state, routers) => {
    state.addRouters = routers
    state.routers = constantRoutes.concat(routers)
  },
  SET_MENU: (state, menu) => {
    state.menu_list = menu
  },
  SET_BUT: (state, menu) => {
    state.but_list = menu
  }
}

const actions = {
  GenerateRoutes({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      listUserMenu(userInfo).then(response => {
        commit('SET_ROUTERS', asyncRouterMap)
        commit('SET_MENU', response.data.menu_list)
        commit('SET_BUT', response.data.but_list)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}

